//
//  ImageScaleViewController.m
//  MansApp
//
//  Created by lanou3g on 15/7/20.
//  Copyright (c) 2015年 ACH. All rights reserved.
//

#import "ImageScaleViewController.h"

@interface ImageScaleViewController ()<UIScrollViewDelegate>
@property (nonatomic, strong) UIScrollView *scrollViewBig;
@property (nonatomic, assign) NSInteger number;
@property (nonatomic,retain) UIPageControl *page;
@property (nonatomic, strong) UIImageView *backImage;
@property (nonatomic, strong) UIImageView *imageView;
@end

@implementation ImageScaleViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBar.hidden = YES;
    self.view.backgroundColor = [UIColor whiteColor];
    
    //设置毛玻璃效果
    self.backImage = [[GroundGlass alloc]initWithFrame:[UIScreen mainScreen].bounds style:UIBlurEffectStyleLight];
    [self.view addSubview:self.backImage];

    //创建一个大的 scrollView
    self.scrollViewBig = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight)];
    self.scrollViewBig.pagingEnabled = YES;
    self.scrollViewBig.contentSize = CGSizeMake(kWidth*self.imageArr.count, kHeight);
    self.scrollViewBig.bounces = YES;
    self.scrollViewBig.delegate = self;
    [self.scrollViewBig setShowsHorizontalScrollIndicator:NO];//隐藏横向滚动条
    CGPoint posotion = CGPointMake(kWidth*self.index, 0);
    [self.scrollViewBig setContentOffset:posotion animated:NO];//设置 scrollView 滚动的具体位置
    [self.view addSubview:self.scrollViewBig];
    for (int i = 0; i< self.imageArr.count; i++)
    {
        //创建一个小的 uiscrollView,再在小的上添加图片
        UIScrollView *scrollViewMin = [[UIScrollView alloc] initWithFrame:CGRectMake(kWidth*i, 0, kWidth, kHeight)];
        scrollViewMin.delegate = self;
        scrollViewMin.minimumZoomScale = 0.5;
        scrollViewMin.maximumZoomScale = 2.0;
        [scrollViewMin setShowsVerticalScrollIndicator:NO];
        [scrollViewMin setShowsHorizontalScrollIndicator:NO];
        AmusementModel *model = [[AmusementModel alloc] init];
        model = self.imageArr[i];
        AmusementModel *oneModel = self.imageArr[0];
        [self.backImage sd_setImageWithURL:[NSURL URLWithString:oneModel.url]];
        self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, kP6Height(0), kWidth, kP6Height(677))];
         self.imageView .contentMode = UIViewContentModeScaleAspectFit;//图片剪切模式
        [self.imageView sd_setImageWithURL:[NSURL URLWithString:model.url]];
        [scrollViewMin addSubview:self.imageView];
        [self.scrollViewBig addSubview:scrollViewMin];
    }
    //创建UIPageControl 对象
    self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(kP6width(100), kP6Height(640), kP6width(175), kP6Height(30))];
    self.page.numberOfPages = self.imageArr.count;

    UITapGestureRecognizer *tapImageView = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageViewAction)];
    [self.scrollViewBig addGestureRecognizer:tapImageView];
    
    
    UIButton *leftBut = [UIButton buttonWithType:UIButtonTypeSystem];//返回
    leftBut.frame = CGRectMake(kP6Height(20), kP6Height(630), kP6width(35), kP6Height(37));
    [leftBut setBackgroundImage:[UIImage imageNamed:@"common_icon_return@3x"] forState:UIControlStateNormal];
    [leftBut addTarget:self action:@selector(leftButAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:leftBut];
    
    UIButton *downImageBut = [UIButton buttonWithType:UIButtonTypeCustom];//下载
    downImageBut.frame = CGRectMake(kP6width(320), kP6Height(625), kP6width(35), kP6Height(40));
    [downImageBut setImage:[UIImage imageNamed:@"common_icon_download@2x"] forState:UIControlStateNormal];
    [downImageBut addTarget:self action:@selector(downImageButAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:downImageBut];
    
}

#pragma mark- 给标题图片加轻拍手势, 点击并跳转
- (void)tapImageViewAction
{
    [self.navigationController  popViewControllerAnimated:NO];
}

//实现代理方法 ：视图滚动就会触发
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    self.page.currentPage =  _scrollViewBig.contentOffset.x / kWidth;  //当前页数 = 大 scrollview 偏移量 / 宽
}

//允许按个视图进行缩放
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return  [scrollView.subviews objectAtIndex:0];
}
//已经进行缩放功能时触发的方法
- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
    UIImageView *imageView = [scrollView.subviews objectAtIndex:0];
    imageView.center = CGPointMake(imageView.superview.frame.size.width/2.0, imageView.superview.frame.size.height / 2.0);
}
//结束减速时触发
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    //循环创建，取出每一个视图对象，滑回原有视图时，恢复初始化大小,如果没有滑到其他视图 不改变
    for (int i = 0; i <self.imageArr.count; i ++ ) {
        if (_page.currentPage != i) {
            UIScrollView *temp = [_scrollViewBig.subviews objectAtIndex:i];
            temp.zoomScale = 1.0;
        }
    }
}

#pragma mark - 将图片下载到本地相册中
- (void)downImageButAction
{
     AmusementModel *model = self.imageArr[self.page.currentPage];
    [self.imageView sd_setImageWithURL:[NSURL URLWithString:model.url]];
    UIImageWriteToSavedPhotosAlbum(self.imageView.image, self, @selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:), nil);
}

- (void)imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    if (!error)
    {
        [MBProgressHUD showError:@"已归为己有"];
    }
    else
    {
        [MBProgressHUD showError:@"肿么办,么有把到"];
    }
}

#pragma mark - 点击 button
- (void)leftButAction//返回
{
    [self.navigationController popViewControllerAnimated:NO];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
